#!/usr/bin/env node

// Suppress punycode deprecation warning before any imports
process.removeAllListeners('warning');
process.on('warning', (warning) => {
  if (warning.name === 'DeprecationWarning' && warning.message.includes('punycode')) {
    return; // Ignore punycode deprecation warnings
  }
  console.warn(warning.name + ': ' + warning.message);
});

import { createMCPServer } from "./server.js";

/**
 * Main entry point for the Codebase RAG MCP Server
 */
async function main() {
  try {
    console.log("🚀 Starting Codebase RAG MCP Server...");
    
    // Create the MCP server
    const server = createMCPServer();
    
    // Start the server with stdio transport (for MCP clients)
    await server.start({
      transportType: "stdio",
    });
    
    console.log("✅ Codebase RAG MCP Server started successfully");
    console.log("📡 Using stdio transport - ready for MCP client connections");
    
  } catch (error) {
    console.error("❌ Failed to start Codebase RAG MCP Server:", error);
    process.exit(1);
  }
}

// Handle graceful shutdown
process.on('SIGINT', () => {
  console.log("\n🛑 Shutting down Codebase RAG MCP Server...");
  process.exit(0);
});

process.on('SIGTERM', () => {
  console.log("\n🛑 Shutting down Codebase RAG MCP Server...");
  process.exit(0);
});

// Start the server
main().catch((error) => {
  console.error("💥 Unhandled error:", error);
  process.exit(1);
});
